<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="配餐单位" prop="mealUnit">
        <el-input v-model="formData.mealUnit" placeholder="请输入配餐单位" />
      </el-form-item>
      <el-form-item label="航班号" prop="flightNo">
        <el-input v-model="formData.flightNo" placeholder="请输入航班号" />
      </el-form-item>
      <el-form-item label="机型" prop="aircraftType">
        <el-select v-model="formData.aircraftType" placeholder="请选择机型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="班期(如:1234567)" prop="schPat">
        <el-input v-model="formData.schPat" placeholder="请输入班期(如:1234567)" />
      </el-form-item>
      <el-form-item label="起飞地点" prop="depLoc">
        <el-input v-model="formData.depLoc" placeholder="请输入起飞地点" />
      </el-form-item>
      <el-form-item label="起飞站点三字代码" prop="depIata">
        <el-input v-model="formData.depIata" placeholder="请输入起飞站点三字代码" />
      </el-form-item>
      <el-form-item label="起飞时间" prop="depTime">
        <el-date-picker
          v-model="formData.depTime"
          type="date"
          value-format="x"
          placeholder="选择起飞时间"
        />
      </el-form-item>
      <el-form-item label="降落地点" prop="arrLoc">
        <el-input v-model="formData.arrLoc" placeholder="请输入降落地点" />
      </el-form-item>
      <el-form-item label="降落站点三字代码" prop="arrIata">
        <el-input v-model="formData.arrIata" placeholder="请输入降落站点三字代码" />
      </el-form-item>
      <el-form-item label="降落时间" prop="arrTime">
        <el-date-picker
          v-model="formData.arrTime"
          type="date"
          value-format="x"
          placeholder="选择降落时间"
        />
      </el-form-item>
      <el-form-item label="公布飞行时长(分钟)" prop="flyDur">
        <el-input v-model="formData.flyDur" placeholder="请输入公布飞行时长(分钟)" />
      </el-form-item>
      <el-form-item label="平均空中实际飞行时间(分钟)" prop="avgActualDur">
        <el-input v-model="formData.avgActualDur" placeholder="请输入平均空中实际飞行时间(分钟)" />
      </el-form-item>
      <el-form-item label="航班性质" prop="flightType">
        <el-select v-model="formData.flightType" placeholder="请选择航班性质">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="商务舱餐谱" prop="bussClassMealQty">
        <el-input v-model="formData.bussClassMealQty" placeholder="请输入商务舱餐谱" />
      </el-form-item>
      <el-form-item label="高级经济舱餐谱" prop="premEcoClassMeal">
        <el-input v-model="formData.premEcoClassMeal" placeholder="请输入高级经济舱餐谱" />
      </el-form-item>
      <el-form-item label="经济舱餐谱" prop="ecoClassMeal">
        <el-input v-model="formData.ecoClassMeal" placeholder="请输入经济舱餐谱" />
      </el-form-item>
      <el-form-item label="机组餐谱" prop="crewMeal">
        <el-input v-model="formData.crewMeal" placeholder="请输入机组餐谱" />
      </el-form-item>
      <el-form-item label="周配餐量" prop="weeklyMealQty">
        <el-input v-model="formData.weeklyMealQty" placeholder="请输入周配餐量" />
      </el-form-item>
      <el-form-item label="所属公司" prop="airlineComp">
        <el-input v-model="formData.airlineComp" placeholder="请输入所属公司" />
      </el-form-item>
      <el-form-item label="所属公司三字代码" prop="airlineIata">
        <el-input v-model="formData.airlineIata" placeholder="请输入所属公司三字代码" />
      </el-form-item>
      <el-form-item label="航线类别" prop="airlineType">
        <el-select v-model="formData.airlineType" placeholder="请选择航线类别">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="执行日期" prop="effectiveDate">
        <el-date-picker
          v-model="formData.effectiveDate"
          type="date"
          value-format="x"
          placeholder="选择执行日期"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="formData.remarks" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="小水放袋/盒" prop="isWaterPkg">
        <el-radio-group v-model="formData.isWaterPkg">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联程航班" prop="isConnFlight">
        <el-radio-group v-model="formData.isConnFlight">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="航食站点" prop="cateringSite">
        <el-input v-model="formData.cateringSite" placeholder="请输入航食站点" />
      </el-form-item>
      <el-form-item label="非标" prop="nonStd">
        <el-input v-model="formData.nonStd" placeholder="请输入非标" />
      </el-form-item>
      <el-form-item label="匹配" prop="isMatche">
        <el-radio-group v-model="formData.isMatche">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="航班量统计" prop="flightCount">
        <el-input v-model="formData.flightCount" placeholder="请输入航班量统计" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { QuarterPlanApi, QuarterPlanVO } from '@/api/distm/quarterplan'

/** 配送管理-季度计划 表单 */
defineOptions({ name: 'QuarterPlanForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  mealUnit: undefined,
  flightNo: undefined,
  aircraftType: undefined,
  schPat: undefined,
  depLoc: undefined,
  depIata: undefined,
  depTime: undefined,
  arrLoc: undefined,
  arrIata: undefined,
  arrTime: undefined,
  flyDur: undefined,
  avgActualDur: undefined,
  flightType: undefined,
  bussClassMealQty: undefined,
  premEcoClassMeal: undefined,
  ecoClassMeal: undefined,
  crewMeal: undefined,
  weeklyMealQty: undefined,
  airlineComp: undefined,
  airlineIata: undefined,
  airlineType: undefined,
  effectiveDate: undefined,
  remarks: undefined,
  isWaterPkg: undefined,
  isConnFlight: undefined,
  cateringSite: undefined,
  nonStd: undefined,
  isMatche: undefined,
  flightCount: undefined
})
const formRules = reactive({
  mealUnit: [{ required: true, message: '配餐单位不能为空', trigger: 'blur' }],
  flightNo: [{ required: true, message: '航班号不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await QuarterPlanApi.getQuarterPlan(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as QuarterPlanVO
    if (formType.value === 'create') {
      await QuarterPlanApi.createQuarterPlan(data)
      message.success(t('common.createSuccess'))
    } else {
      await QuarterPlanApi.updateQuarterPlan(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    mealUnit: undefined,
    flightNo: undefined,
    aircraftType: undefined,
    schPat: undefined,
    depLoc: undefined,
    depIata: undefined,
    depTime: undefined,
    arrLoc: undefined,
    arrIata: undefined,
    arrTime: undefined,
    flyDur: undefined,
    avgActualDur: undefined,
    flightType: undefined,
    bussClassMealQty: undefined,
    premEcoClassMeal: undefined,
    ecoClassMeal: undefined,
    crewMeal: undefined,
    weeklyMealQty: undefined,
    airlineComp: undefined,
    airlineIata: undefined,
    airlineType: undefined,
    effectiveDate: undefined,
    remarks: undefined,
    isWaterPkg: undefined,
    isConnFlight: undefined,
    cateringSite: undefined,
    nonStd: undefined,
    isMatche: undefined,
    flightCount: undefined
  }
  formRef.value?.resetFields()
}
</script>